package net.controller;

import net.comment.ResponseMessage;
import net.mapper.UserMapper;
import net.pojo.*;
import net.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/findAllArtile")
    public String findAllArtile(Model model){
        List<Artile> artileList = adminService.findAll();
        List<Artile> search = adminService.searchNum();
        model.addAttribute("artileList",artileList);
        model.addAttribute("search",search);
        return "/client/artileIndex.jsp";
    }

    @RequestMapping("/search")
    @ResponseBody
    public List<Artile> search(String ptags,String ptitle){
        if (ptags.equals("--选择类别--") ){
            List<Artile> artileList = adminService.search(null,ptitle);
            return artileList;
        }
        List<Artile> artileList = adminService.search(ptags,ptitle);
        return artileList;
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer aid){
        int i = adminService.deleteById(aid);
        return "redirect:/admin/findAllArtile.do";
    }

    @RequestMapping("/findById")
    public String findById(Integer aid,Model model){
        Artile artile = adminService.findById(aid);
        List<Artile> search = adminService.searchNum();
        model.addAttribute("artile",artile);
        model.addAttribute("search",search);
        return "/client/artileChange.jsp";
    }

    @RequestMapping("/updateArtile")
    @ResponseBody
    public ResponseMessage updateArtile(Artile artile, @RequestParam(value = "uploadFile",required = false)MultipartFile uploadFile, HttpServletRequest request){
        if (artile.getTags().equals("--选择类别--")){
            artile.setTags("未选分类");
        }

        int serviceResponse = adminService.updateArtile(artile,uploadFile,request);
        if (serviceResponse > 0){
            return ResponseMessage.createSuccessMessage("文章修改成功");
        }
        return ResponseMessage.createErrorMessage("文章修改失败");
    }

    @RequestMapping("/addArtile")
    @ResponseBody
    public ResponseMessage addArtile(Artile artile,@RequestParam(value = "uploadFile",required = false)MultipartFile uploadFile, HttpServletRequest request,HttpSession session){
        User user = (User) session.getAttribute("user");
        artile.setAuthor(user.getUsername());
        if (artile.getTags().equals("--选择类别--")){
            artile.setTags("全部分类");
        }
        if (artile.getTitle().equals("") ||  artile.getPage().equals("")){
            return ResponseMessage.createErrorMessage("文章添加失败");
        }
        int i = adminService.addArtile(artile, uploadFile, request);
        if (i > 0){
            return ResponseMessage.createSuccessMessage("文章添加成功");
        }
        return ResponseMessage.createErrorMessage("文章添加失败");
    }

    @RequestMapping("/addUI")
    public String addUI(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Artile> search = adminService.searchNum();
        model.addAttribute("search",search);
        model.addAttribute("username",user.getUsername());
        return "/client/artileInsert.jsp";
    }

    @RequestMapping("/findAllDicuss")
    public String findAllDicuss(Model model){
        List<Dicuss> dicussList = adminService.findAllDicuss();
        for (Dicuss dicuss : dicussList){
            User user1 = userMapper.findById(dicuss.getDuid());
            dicuss.setUser(user1);
        }
        model.addAttribute("dicussList",dicussList);
        return "/client/commentIndex.jsp";
    }

    @RequestMapping("/deleteDicuss")
    public String deleteDicuss(Integer did){
        int i = adminService.deleteDicuss(did);
        return "redirect:/admin/findAllDicuss.do";
    }

    @RequestMapping("/findAllComment")
    public String findAllComment(Model model){
        List<Comments> commentsList = adminService.findAllComment();
        for (Comments comments : commentsList){
            User user1 = userMapper.findById(comments.getUsid());
            comments.setUser(user1);
        }
        model.addAttribute("commentsList",commentsList);
        return "/client/MesIndex.jsp";
    }

    @RequestMapping("/deleteComments")
    public String deleteComments(Integer cid){
        int i = adminService.deleteComments(cid);
        return "redirect:/admin/findAllComment.do";
    }

    @RequestMapping("/findLinks")
    public String findLinks(Model model){
        List<Links> linksList = adminService.findLinks();
        model.addAttribute("linksList",linksList);
        return "/client/LinkIndex.jsp";
    }

    @RequestMapping("/deleteLinks")
    public String deleteLinks(Integer lid){
        int i = adminService.deleteLinks(lid);
        return "redirect:/admin/findLinks.do";
    }

    @RequestMapping("/updateLinks")
    @ResponseBody
    public ResponseMessage updateLinks(@RequestBody Links links){
        int i = adminService.updateLinks(links);
        if (i > 0){
            return ResponseMessage.createSuccessMessage("友链修改成功");
        }
        return ResponseMessage.createErrorMessage("友链修改失败");
    }

    @RequestMapping("/findLinkById")
    public String findLinkById(Integer lid,Model model){
        Links link = adminService.findLinkById(lid);
        model.addAttribute("link",link);
        return "/client/LinkChange.jsp";
    }
}
